package com.superhelper.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.type.TypeReference;
import com.superhelper.app.common.Consts;
import com.superhelper.app.controller.base.AbstractController;
import com.superhelper.app.domain.Resp;
import com.superhelper.app.service.DeliverService;
import com.superhelper.app.service.deliver.IDeliverService;
import com.superhelper.common.domain.AuserDeliver;
import com.superhelper.common.domain.DadaReasons;
import com.superhelper.common.domain.DeliverAccount;
import com.superhelper.common.domain.WifiAccount;

@Controller
@RequestMapping("v1/deliver")
public class DeliverController extends AbstractController {

    @Autowired
    private DeliverService deliverService;
    @Autowired
    private IDeliverService dadaService;

    /**
     * 绑定
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("bound")
    @ResponseBody
    public Resp bound(@RequestParam Long userId, @RequestParam Integer deliverType, @RequestParam String deliverUid, @RequestParam String deliverShopNo) {
	DeliverAccount entity = new DeliverAccount();
	entity.setAuserId(userId);
	entity.setDeliverType(deliverType);
	entity.setDeliverUid(deliverUid);
	entity.setDeliverShopNo(deliverShopNo);
	int res = deliverService.addAccount(entity);
	return res > 0 ? success("绑定成功") : fail("绑定失败");
    }

    /**
     * 设置
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("conf/info")
    @ResponseBody
    public Resp set(@RequestParam Long userId) {
	AuserDeliver entity = deliverService.getAuserDeliver(userId);
	return success(entity);
    }

    /**
     * 设置保存
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("conf/save")
    @ResponseBody
    public Resp setSave(@RequestParam Long userId, AuserDeliver entity) {
	entity.setAuserId(userId);
	entity.setState(1);
	int res = deliverService.addSet(entity);
	return res > 0 ? success("保存成功") : fail("保存失败");
    }

    /**
     * 订单列表
     * 
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Resp list(@RequestParam Long userId, @RequestParam Integer status, @RequestParam Long timestamp, @RequestParam Integer pageSize) {
	return deliverService.getDeliverRecordList(userId, timestamp, pageSize, status);
    }

    /**
     * 达达增加小费
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("tips")
    @ResponseBody
    public Resp tips(@RequestParam Long userId, @RequestParam Long id, @RequestParam Double tips) throws Exception {
	int res = deliverService.addTips(id, tips);
	return res > 0 ? success("操作成功") : fail("操作失败");
    }

    /**
     * 取消订单原因列表
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("reason")
    @ResponseBody
    public Resp reasonList(@RequestParam Long userId, @RequestParam Long id) {
	String reason_key = "reasonList_key";
	List<DadaReasons> list = redisService.get(reason_key, new TypeReference<List<DadaReasons>>() {
	});
	if (list == null || list.size() == 0) {
	    list = deliverService.getReasonsList(id);
	    redisService.set(reason_key, Consts.DAY_AGE * 7, list);
	}
	return success(list);
    }

    /**
     * 取消订单
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("cancel")
    @ResponseBody
    public Resp cancel(@RequestParam Long userId, @RequestParam Long id, @RequestParam Integer reasonId) throws Exception {
	int res = deliverService.cancel(id, reasonId);
	return res > 0 ? success("操作成功") : fail("操作失败");
    }

    /**
     * wifi打印机列表
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("wifi/list")
    @ResponseBody
    public Resp wifiList(@RequestParam Long userId) {
	List<WifiAccount> list = deliverService.getWifiAccountList(userId);
	return success(list);
    }

    /**
     * 绑定wifi
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping("wifi/bound")
    @ResponseBody
    public Resp wifiBound(@RequestParam Long userId, @RequestParam String sn, @RequestParam String key) {
	WifiAccount entity = new WifiAccount();
	entity.setAuserId(userId);
	entity.setSecret(key);
	entity.setSn(sn);
	entity.setType(0);
	int res = deliverService.addWifiAccount(entity);
	return res > 0 ? success("绑定成功") : fail("绑定失败");
    }

}
